package controller;

import business.ebi.CommodityEBI;
import business.factory.BusinessFactory;
import common.WebUtils;
import entity.Commodity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

@WebServlet(name = "EditServlet",urlPatterns = "/editServlet")
public class EditController extends HttpServlet {

    private final CommodityEBI ebi = BusinessFactory.getCommodityEBI();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("method");
        if (action!=null){
            try {
                Method method =this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
                method.invoke(this,req,resp);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }else {
            this.doPost(req, resp);
        }
    }

    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        Commodity commodity = null;
        try {
            commodity = WebUtils.copyParamToBean(req, new Commodity());
            ebi.insert(commodity);
            req.getRequestDispatcher("/CommodityListController?method=doPost").forward(req, resp);
        } catch (NullPointerException e) {
            resp.getWriter().write("<script language=javascript>alert('" + "您有未填写的数据" + "');window.history.back()</script>");
        } catch (SQLException e) {
            //在插入的时候可能会出现主键商品码已经重复的情况，此时弹出提示框告诉用户点击返回重新修改，此处是通过调用back()方法返回页面
            resp.getWriter().write("<script language=javascript>alert('" + "输入的商品条形码已经被使用!点击返回重新修改" + "');window.history.back()</script>");
        }
    }

    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, SQLException {
        resp.setContentType("text/html;charset=utf8");
        String oldcId = req.getParameter("oldcId");
        Commodity commodity = WebUtils.copyParamToBean(req, new Commodity());
        try {
            ebi.update(oldcId, commodity);
            req.getRequestDispatcher("/CommodityListController?method=doPost").forward(req, resp);
        } catch (SQLException e) {
            //在更新的时候可能会出现主键商品码已经重复的情况，此时弹出提示框告诉用户点击返回重新修改，此处是通过调用back()方法返回页面
            resp.getWriter().write("<script language=javascript>alert('" + "输入的商品条形码已经被使用!点击返回重新修改" + "');window.history.back()</script>");
        }
    }
}